# map()的用法：接收两个参数，一个是函数，一个是Iterator,map将传入的函数依次作用到序列的每一个元素，并把结果作为新的Iterator返回
def f(x):
    return x * x


r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(r))

L = []
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    L.append(f(n))
print(L)

print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

# reduce的用法
# reduce(f, [1, 2, 3, 4]) = f(f(f(1, 2), 3), 4)


from functools import reduce


def add(x, y):
    return x + y


print(reduce(add, [1, 3, 5, 7, 9]))

from functools import reduce


def fn(x, y):
    return x * 10 + y


print(reduce(fn, [1, 3, 5, 7, 9]))

# 把字符串str转换为int类型


from functools import reduce


def fn(x, y):
    return x * 10 + y


def char2num(s):
    digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return digits[s]


print(reduce(fn, map(char2num, '123456789')))


# 1.名字正确格式
def normalize(name):
    return name.capitalize()


L1 = ['Adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)

# 2.接受一个list并利用reduce求积
from functools import reduce


def prod(s):
    def sn(x, y):
        return x * y

    return reduce(sn, s)


print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

# 3.把字符串‘123.456'转换成浮点数123.456

from functools import reduce


def str2float(s):
    result = []
    for i in range(0, len(s)):
        if s[i] == '.':
            n = isDBNM
        else:
            result.append(s[i])

            result = list(map(lambda x, y: float(x), result))
            return reduce(lambda x, y: x*10 + y, result) * pow(10, n*-1)


print("1234.567")
